package com.fqgj.xjd.user.service.impl;

import com.fqgj.common.api.Response;
import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.xjd.user.client.UserEmailService;
import com.fqgj.xjd.user.client.UserInfoService;
import com.fqgj.xjd.user.client.enums.AuthStatusEnum;
import com.fqgj.xjd.user.client.response.UserAuthEmail;
import com.fqgj.xjd.user.common.enums.UserErrorCodeEnum;
import com.fqgj.xjd.user.dao.UserAuthEmailDao;
import com.fqgj.xjd.user.dao.UserAuthStatusDao;
import com.fqgj.xjd.user.dao.UserBaseDao;
import com.fqgj.xjd.user.entity.UserAuthEmailEntity;
import com.fqgj.xjd.user.entity.UserAuthStatusEntity;
import com.fqgj.xjd.user.entity.UserBaseEntity;
import com.qianli.user.ro.UserStateCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("userEmailService")
public class UserAuthEmailServiceImpl implements UserEmailService {

    @Autowired
    UserAuthEmailDao userAuthEmailDao;

    @Autowired
    UserInfoService userInfoService;

    @Autowired
    UserBaseDao userBaseDao;

    @Autowired
    private UserAuthStatusDao userAuthStatusDao;


    @Override
    public Response<UserAuthEmail> getUserEmail(String userCode) {
        UserAuthEmail authEmail = new UserAuthEmail();
        UserAuthEmailEntity userAuthEmailEntity = getUserAuthEmailEntity(userCode);
        UserAuthEmailEntity entity = Optional.ofNullable(userAuthEmailEntity).orElseGet(UserAuthEmailEntity::new);
        BeanUtils.copyProperties(entity, authEmail);
        return Response.ok().putData(authEmail);
    }


    @Override
    public Response addUserEmail(UserAuthEmail userAuthEmail) {
        //LOGGER.info("addUserEmail: {}", JSONObject.toJSONString(userAuthEmail));
        if (StringUtils.isBlank(userAuthEmail.getUserCode()) || StringUtils.isBlank(userAuthEmail.getEmail())) {
            return Response.error(UserErrorCodeEnum.PARAM_ERROR);
        }
        String userCode = userAuthEmail.getUserCode();
        UserAuthEmailEntity userAuthEmailEntity = getUserAuthEmailEntity(userCode);
        //LOGGER.info("userAuthEmailEntity: {}", JSONObject.toJSONString(userAuthEmailEntity));
        if (Objects.nonNull(userAuthEmailEntity)) {
            userAuthEmailEntity.setEmail(userAuthEmail.getEmail())
                    .setStatus(userAuthEmail.getStatus())
                    .setGmtModified(new Date());
            userAuthEmailDao.updateByPrimaryKey(userAuthEmailEntity);
        } else {
            userAuthEmailEntity = new UserAuthEmailEntity();
            BeanUtils.copyProperties(userAuthEmail, userAuthEmailEntity);
            userAuthEmailDao.insert(userAuthEmailEntity);
        }

        UserBaseEntity oldEntity = userBaseDao.selectByUserCode(userCode);
        if (oldEntity != null) {
            oldEntity.setEmail(userAuthEmail.getEmail());
            userBaseDao.updateByPrimaryKey(oldEntity);
        } else {
            UserBaseEntity entity = new UserBaseEntity();
            entity.setUserCode(userAuthEmail.getUserCode());
            entity.setEmail(userAuthEmail.getEmail());
            userBaseDao.insert(entity);
        }
        UserAuthStatusEntity userAuthStatusEntity = userAuthStatusDao.getUserAuthStatusByUserCode(userCode);
        userAuthStatusEntity.setEmailStatus(1);
        userAuthStatusDao.updateByPrimaryKey(userAuthStatusEntity);
        return Response.ok();
    }

    @Override
    public Response<String> getUserCodeByEmail(String email) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("email", email);
        paramMap.put("deleted", 0);
        paramMap.put("status", AuthStatusEnum.AUTH_SUCCESS.getStatus());
        List<UserAuthEmailEntity> list = userAuthEmailDao.selectByParams(paramMap);
        if (CollectionUtils.isNotEmpty(list)) {
            UserAuthEmailEntity userAuthEmailEntity = list.get(list.size() - 1);
            return Response.ok().putData(userAuthEmailEntity.getUserCode());
        }

        return Response.error(UserStateCode.USER_NOT_EXITS.getMsg());
    }

    private UserAuthEmailEntity getUserAuthEmailEntity(String userCode) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("userCode", userCode);
        List<UserAuthEmailEntity> list = userAuthEmailDao.selectByParams(paramMap);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }
}
